// 引入路由和storage工具函数
import storage from '@/utils/storage'
import router from "@/router"
import { Message } from 'element-ui'

let lastTime = new Date().getTime()
let currentTime = new Date().getTime()
let timeOut = 3 * 60 * 1000  //设置超时时间: 30分钟

storage.setItem("lastTime", new Date().getTime())

window.onload = function () {
    window.document.onmousedown = function () {
        storage.setItem("lastTime", new Date().getTime())
    }
};

function checkTimeout() {
    console.log(new Date(currentTime).toLocaleTimeString(),"currentTime");
    // console.log(lastTime,"lastTime");
    console.log(new Date(lastTime).toLocaleTimeString(),"lastTime");
    console.log(new Date(timeOut).toLocaleTimeString(),"timeOut");
    console.log(new Date(currentTime - lastTime).toLocaleTimeString(),"currentTime - lastTime");
    currentTime = new Date().getTime()		//更新当前时间
    lastTime = storage.getItem("lastTime");

    if (currentTime - lastTime > timeOut) { //判断是否超时
        Message({
            duration: 2000,
            showClose: true,
            message: "您长时间未操作, 已跳转到首页",
            type: 'warning',
        });
        // 清除storage的数据(登陆信息和token)
        storage.clear()
        // 跳到登陆页
        // if(router.currentRouter.name == 'museums') return // 当前已经是登陆页时不做跳转
        router.push({ name: 'museums' })
    }
}

export default function () {
    /* 定时器 间隔30秒检测是否长时间未操作页面 */
    window.setInterval(checkTimeout, 1000);
}
